﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CookieExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		//if (!Page.IsPostBack)
		//{
		//    if (Response.Cookies["userinfo"] != null)
		//    {
		//        txtUsername.Text = Request.Cookies["userinfo"]["username"];
		//        txtPassword.Text = Request.Cookies["userinfo"]["password"];
		//        chkRememberPassword.Checked = true;
		//    }
		//}

		if(!this.Page.IsPostBack)
		{
			if (Response.Cookies["userinfo"] != null)
			{
				txtUsername.Text = Request.Cookies["userinfo"]["username"];
				txtPassword.Text = Request.Cookies["userinfo"]["password"];
				chkRememberPassword.Checked = true;
			}
		}

    }
	protected void btnLogin_Click(object sender, EventArgs e)
	{
		//if (chkRememberPassword.Checked)
		//{
		//    Response.Cookies["userinfo"]["username"] = txtUsername.Text.Trim();
		//    Response.Cookies["userinfo"]["password"] = txtPassword.Text.Trim();
		//}
		//Response.Redirect("CookieExample.aspx");

		if(chkRememberPassword.Checked)
		{
			//Response.Cookies["userinfo"]["username"] = txtUsername.Text;
			//Response.Cookies["userinfo"]["password"] = txtPassword.Text;
			//Response.Cookies["userinfo"].Expires = DateTime.Now.AddDays(1);
			HttpCookie userinfoCookie = new HttpCookie("userinfo");
			userinfoCookie.Expires = DateTime.Now.AddDays(1);
			userinfoCookie["username"] = txtUsername.Text;
			userinfoCookie["password"] = txtPassword.Text;

			Response.Cookies.Add(userinfoCookie);
			//Response.AppendCookie(userinfoCookie);
		}
	}
}
